<template>
  <div class="contractcorrectionadd" v-loading="loading">
    <div class="contractcorrectionadd-mode">
      <div>加工单添加时间</div>
      <div>
        2021年
        <el-select v-model="data.month" placeholder="请选择月份">
          <el-option
            v-for="item in 5"
            :key="item"
            :label="'0' + item"
            :value="'0' + item"
          ></el-option>
        </el-select>
      </div>
    </div>
    <div class="contractcorrectionadd-mode">
      <div>核价单信息</div>
      <div class="contractcorrectionadd-mode-two">
        <div>
          <div>成衣成本加价总价：</div>
          <el-input
            v-model="data.unit_price"
            placeholder="请输入成衣成本加价总价"
            type="number"
            @change="onChangePrice"
          ></el-input>
        </div>
        <div>
          <div>绣花成本加价总价：</div>
          <el-input
            v-model="data.embroidery_price"
            placeholder="请输入绣花成本加价总价"
            type="number"
            @change="onChangePrice"
          ></el-input>
        </div>
        <div>
          <div>绣花厂：</div>
          <el-select
            v-model="data.embroidery_supplier_id"
            placeholder="请选择"
            @change="onChange"
            clearable
          >
            <el-option
              v-for="item in factoryList"
              :key="item.supplier_id"
              :label="item.factory_name"
              :value="item.supplier_id"
            ></el-option>
          </el-select>
        </div>
        <div>
          <div>成本加价总价：</div>
          <el-input
            v-model="totalPrice"
            disabled
            placeholder="成本加价总价"
          ></el-input>
        </div>
      </div>
    </div>
    <div class="contractcorrectionadd-mode">
      <div>详细信息</div>
      <div>
        <el-table :data="tableData" style="width: 100%" border>
          <el-table-column
            label="SKU"
            align="center"
            prop="sku_id"
          ></el-table-column>
          <el-table-column
            label="数量"
            align="center"
            prop="num"
          ></el-table-column>
        </el-table>
      </div>
      <div class="contractcorrectionadd-mode-totalnumber">
        <div>总数量（件）：</div>
        <el-input v-model="skunum" disabled placeholder="总数量"></el-input>
      </div>
      <div class="contractcorrectionadd-mode-totalnumber">
        <div>总金额（元）：</div>
        <el-input
          v-model="totalPriceAll"
          disabled
          placeholder="总金额"
        ></el-input>
      </div>
    </div>
    <el-button type="primary" style="margin-top: 20px" @click="onSave"
      >保存</el-button
    >
  </div>
</template>

<script>
import {
  postAdminPricinggetPurchaseinDetail,
  postAdminPricinggetaddProcessing_code,
  postHomeCodeGetFactory,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      form: {
        po_id: "",
        page: 1,
        size: 10000,
      },
      tableData: [],
      total: 1,
      data: {
        po_id: "",
        month: "",
        unit_price: "",
        embroidery_price: "",
        embroidery_supplier_id:""
      },
      totalPrice: "",
      skunum: "",
      totalPriceAll: "",
      factoryList: [],
    };
  },
  activated() {
    this.postHomeCodeGetFactory()
    if (this.$route.query.po_id) {
      this.data.po_id = this.$route.query.po_id;
      this.form.po_id = this.$route.query.po_id;
      let form = this.form;
      this.postAdminPricinggetPurchaseinDetail(form);
    }
  },
  methods: {
    // 绣花厂列表
    postHomeCodeGetFactory() {
      let obj = {
        type: 2,
        page: 1,
        size: 10000,
      };
      postHomeCodeGetFactory(obj).then((res) => {
        this.factoryList = res.data;
      });
    },
    // 价格改变时
    onChangePrice() {
      let data = this.data;
      if (data.unit_price) {
        this.totalPrice = (
          Number(data.unit_price) + Number(data.embroidery_price || 0)
        ).toFixed(2);
        this.totalPriceAll = (
          Number(data.unit_price) * Number(this.skunum)
        ).toFixed(2);
      } else {
        this.totalPrice = "";
        this.totalPriceAll = "";
      }
    },
    // 保存
    onSave() {
      let data = this.data;
      if (data.month) {
        if (data.unit_price) {
          data.embroidery_price = data.embroidery_price
            ? data.embroidery_price
            : 0;
          this.loading = true;
          postAdminPricinggetaddProcessing_code(this.data)
            .then((res) => {
              this.$message.success("保存成功");
              this.$router.go(-1);
              this.loading = false;
            })
            .catch((err) => {
              this.loading = false;
            });
        } else {
          this.$warning("请输入成衣成本加价总价");
        }
      } else {
        this.$warning("请选择月份");
      }
    },
    // 采购列表
    async postAdminPricinggetPurchaseinDetail(data) {
      this.loading = true;
      this.form = data;
      postAdminPricinggetPurchaseinDetail(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.tableData = res.data.skulist;
          this.skunum = res.data.skunum;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.contractcorrectionadd {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
  .contractcorrectionadd-mode {
    border-bottom: 1px solid #eee;
    padding: 30px 0;
    > div:nth-child(1) {
      font-weight: 800;
      padding-bottom: 20px;
    }
    .contractcorrectionadd-mode-two {
      > div {
        display: flex;
        align-items: center;
        margin-bottom: 20px;
        > div {
          width: 160px;
        }
        /deep/ .el-input {
          width: 300px;
        }
      }
    }
    .contractcorrectionadd-mode-totalnumber {
      margin-top: 20px;
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      > div {
        width: 120px;
      }
      /deep/ .el-input {
        width: 300px;
      }
    }
  }
}
</style>
